import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pixelyear/bean/ColorBean.dart';
import 'package:pixelyear/sql/ColorSqlUtils.dart';
import 'package:pixelyear/sql/Sql.dart';
import 'package:pixelyear/utils/ImageLoad.dart';
import 'package:pixelyear/utils/constant/AssetsConstant.dart';
import 'package:pixelyear/utils/constant/IconConstant.dart';
import 'package:pixelyear/utils/constant/YxsColor.dart';
import 'package:pixelyear/utils/extension/ContextUtils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pixelyear/widget/text/PaddingTextWidget.dart';

class HomeListDialog extends StatefulWidget {
  final int year;
  final int month;
  final int day;
  final Function(int mood, int chooseColor) tap;
  final Function toNote;
  final Function toEdit;

  HomeListDialog(
      this.year, this.month, this.day, this.tap, this.toNote, this.toEdit);

  @override
  State<StatefulWidget> createState() {
    return HomeListState();
  }
}

class HomeListState extends State<HomeListDialog> {
  int chooseIndex = 0;
  List<ColorBean> colorList = [];
  ColorSqlUtils colorUtils;

  @override
  void initState() {
    super.initState();
    init();
  }

  void init() async {
    colorUtils = Sql.getColorUtils();
    var data = await colorUtils.getData();
    setState(() {
      colorList = data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return buildHomeListDialog(context, widget.year, widget.month, widget.day);
  }

  Widget buildHomeListDialog(
      BuildContext context, int year, int month, int day) {
    return WillPopScope(
      child: Dialog(
        child: _build(context, year, month, day),
      ),
      onWillPop: () async {
        return true;
      },
    );
  }

  _build(BuildContext context, int year, int month, int day) {
    return Container(
      height: context.windowHeight * 0.6,
      child: Column(
        children: [
          _buildTitle(year, month, day),
          _buildImageList(),
          _buildColorList(),
        ],
      ),
    );
  }

  _buildTitle(int year, int month, int day) {
    return Container(
      height: 30.w,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
            child: Text(
              "$year - $month - $day",
              style: TextStyle(
                color: YxsColor.black333,
                fontSize: 18.sp,
              ),
            ),
            top: 10.w,
          ),
          Positioned(
            child: Text(
              "编辑",
              style: TextStyle(
                color: YxsColor.blue2595e4,
                fontSize: 12.sp,
              ),
            ),
            right: 12.w,
            top: 12.w,
          ),
        ],
      ),
    );
  }

  _buildImageList() {
    return Container(
      height: 45.w,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemBuilder: (ctx, index) {
          return _buildImageItem(ctx, index);
        },
        itemCount: IconConstant.mood.length,
      ),
    );
  }

  _buildImageItem(ctx, index) {
    return GestureDetector(
      onTap: () {
        setState(() {
          print('点击事件：$index');
          chooseIndex = index;
        });
      },
      child: Container(
        height: 45.w,
        padding: EdgeInsets.all(10.w),
        child: Icon(
          IconConstant.mood[index],
          size: 17.sp,
          color: chooseIndex == index ? YxsColor.blue2595e4 : YxsColor.black333,
        ),
      ),
    );
  }

  _buildColorList() {
    return Flexible(
      child: ListView.builder(
        itemBuilder: (ctx, index) {
          return index == 0
              ? _buildFirstColorItem(ctx, index)
              : _buildColorItem(ctx, index);
        },
        itemCount: colorList.length + 1,
      ),
    );
  }

  _buildFirstColorItem(ctx, index) {
    return GestureDetector(
      onTap: () {
        widget.toNote();
      },
      child: Container(
        margin: EdgeInsets.only(left: 15.w, top: 10.w, bottom: 10.w),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ImageLoad.loadAssets(AssetsConstant.note,
                height: 40.w, width: 40.w),
            Expanded(
              child: PaddingTextWidget(
                '可以添加笔记哦～',
                style: TextStyle(fontSize: 14.sp, color: YxsColor.black333),
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildColorItem(ctx, index) {
    return GestureDetector(
      onTap: () {
        if (chooseIndex == -1) {
          Fluttertoast.showToast(msg: "请选择心情～");
        } else
          widget.tap(IconConstant.mood[chooseIndex].codePoint,
              colorList[index - 1].color);
      },
      child: Container(
        margin: EdgeInsets.only(left: 15.w, top: 10.w, bottom: 10.w),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 40.w,
              height: 40.w,
              decoration: BoxDecoration(
                color: Color(colorList[index - 1].color),
                borderRadius: new BorderRadius.circular((5.w)),
              ),
            ),
            Expanded(
              child: PaddingTextWidget(
                colorList[index - 1].text,
                style: TextStyle(fontSize: 14.sp, color: YxsColor.black333),
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
              ),
            )
          ],
        ),
      ),
    );
  }
}
